package com.jack.leetcode.每日一题;

import java.util.*;

/***
 * @author zhangjie
 * @date 2020/5/12 21:28
 */
public class 最小栈 {

    static class MinStack {
        Stack<Integer> helper;
        Stack<Integer> stack;

        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<>();
            helper = new Stack<>();
        }

        public void push(int x) {
            stack.push(x);
            if(helper.isEmpty()||helper.peek() >= x){
                helper.push(x);
            }
        }

        public void pop() {
            if(!stack.isEmpty()){
                int top = stack.pop();
                if(helper.peek()==top){
                    helper.pop();
                }
            }

        }

        public int top() {
            return stack.peek();
        }

        public int getMin() {
            return helper.peek();
        }
    }

    public static void main(String[] args) {
        MinStack obj = new MinStack();
        obj.push(2);
        obj.push(0);
        System.out.println(obj.getMin());
        obj.push(3);
        System.out.println(obj.getMin());
        obj.push(0);
        obj.pop();
        obj.pop();
        obj.pop();
        System.out.println(obj.getMin());
    }
}
